package com.mryang.hds.service.impl;

import com.mryang.hds.configs.AppException;
import com.mryang.hds.entity.Notify;
import com.mryang.hds.entity.User;
import com.mryang.hds.mapper.NotifyMapper;
import com.mryang.hds.service.INotifyService;
import com.mryang.hds.utils.DateUtil;
import com.mryang.hds.utils.ResultCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName NotifyServiceImpl.java
 * @Description TODO
 * @createTime 2023年03月22日 21:07:00
 */

@Service
public class NotifyServiceImpl implements INotifyService {

    @Resource
    NotifyMapper notifyMapper;

    @Override
    public List<Notify> getMesByUserId(Integer userId) {
        try {
            return notifyMapper.getMesByUserId(userId);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }

    }

    @Override
    public List<Notify> getNotifys(User token ) {
        try {
            return notifyMapper.getMesByCreateId(token);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }
    }

    @Override
    public void editById(Notify notify) {
        try {
            notifyMapper.editById(notify);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }
    }

    @Override
    public void deleteNotify(Integer nid) {
        try {
            notifyMapper.deleteNotify(nid);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }
    }

    @Override
    public List<Notify> getNotifyByUID(Integer uid) {
        try {
            return notifyMapper.getNotifyByUID(uid);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }
    }

    @Override
    public void editLookById(Integer nid) {
        try {
            String looktime = DateUtil.dateToString(new Date(), "yyyy-MM-dd");
            notifyMapper.editLookTimeById(nid,looktime);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }
    }

    @Override
    public List<Notify> searchNotify(Notify notify,User token) {
        try {

            if (token.getStatus() != 0){
                notify.setCreateId(token.getUserId());
            }

            return notifyMapper.searchNotify(notify);

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.Notify_FAIL);
        }
    }
}
